package com.ddd.test.aggregate.aggrsales;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.ddd.test.aggregate.aggrsales.entity.SalesAreaEntity;

//import net.sf.oval.constraint.NotBlank;
//import net.sf.oval.constraint.NotEmpty;
//import net.sf.oval.constraint.NotNull;
//import net.sf.oval.constraint.Size;

/**
 * Sales聚合根
 *
 * 
 */
public class SalesEntity implements Serializable {
 
//  @NotNull(errorCode = "001", message = "[code]${parameter.not.null}")
//  @NotEmpty(errorCode = "001", message = "[code]${parameter.not.null}")
//  @NotBlank(errorCode = "001", message = "[code]${parameter.not.null}")
  private String code;

//  @Size(min = 5, max = 50, errorCode = "001", message = "[name]${parameter.size.invalid}")
  private String name;

  private String description;

  private Integer level;
  
  private List<SalesAreaEntity> salesAreaEntityList;

  private static final long serialVersionUID = 1L;

  //工厂方法
  public static SalesEntity createSalesEntity(String code,String name,String description,Integer level,List<String> areaList){
      
      return new SalesEntity(code,name,description,level,areaList);
  }
  
  protected SalesEntity(String code,String name,String description,Integer level,List<String> areaList){
      this.code = code;
      this.name = name;
      this.description = description;
      this.level = level;
      this.salesAreaEntityList = new ArrayList<SalesAreaEntity>();
      for(String area:areaList) {
    	  this.salesAreaEntityList.add(new SalesAreaEntity(area));
      }
  }
  
  public String getCode() {
    return code;
  }

  public void setCode(String code) {
    this.code = code == null ? null : code.trim();
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name == null ? null : name.trim();
  }

  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description == null ? null : description.trim();
  }

  public Integer getLevel() {
    return level;
  }

  public void setLevel(Integer level) {
    this.level = level ;
  }
  
  public List<SalesAreaEntity> getSalesAreaEntityList() {
	return salesAreaEntityList;
  }

  public void setSalesAreaEntityList(List<SalesAreaEntity> salesAreaEntityList) {
	this.salesAreaEntityList = salesAreaEntityList ;
  }
  
  /**
   * 改名
   *
   * 
   */
  public SalesEntity changeLevel(Integer level) {
  
    if (level > this.getLevel()+1) {
      throw new IllegalStateException("不允许越级");
    }
    this.setLevel(level);
    return this;
  }

  
  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append(getClass().getSimpleName());
    sb.append(" [");
    sb.append("Hash = ").append(hashCode());
    sb.append(", code=").append(code);
    sb.append(", name=").append(name);
    sb.append(", description=").append(description);
    sb.append(", level=").append(level);
    sb.append(", serialVersionUID=").append(serialVersionUID);
    sb.append("]");
    return sb.toString();
  }
}